package com.example.administrator.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private BroadcastReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void buttonClick(View view) {
        int id= view.getId();
        Log.e(TAG,"buttonClick:"+id);
        switch (id){
            case R.id.id_bth_register:
            {
                IntentFilter filter = new IntentFilter();
                filter.addAction("zhaoshilong");
                MyBroadCastReceiver myReceiver =
                        new MyBroadCastReceiver();
                registerReceiver(myReceiver,filter);
                break;
            }
            case R.id.id_bth_send:
            {
                Intent intent = new Intent();
                intent.setAction("zhaoshilong");
                sendBroadcast(intent);
                break;
            }
            case R.id.id_bth_unregister:
            {
                unregisterReceiver(myReceiver);
                break;
            }
        }
    }
    class  MyBroadCastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG,"onReceive:"+"");
        }
    }
}
